<template>
  <div v-loading="loading" class="m--15">
    <el-carousel class="home" trigger="click" height="350px" :autoplay="false">
      <el-carousel-item v-for="item in 1" :key="item">
        <div class="banner" :class="item" />
      </el-carousel-item>
    </el-carousel>
    <div class="pad90">
      <div class="recommend">
        <div class="b-tit">
          <i class="el-icon-star-on" />
          {{ $t('精品推荐') }}
        </div>
        <div class="re-main-flex">
          <div v-for="(v,index) in 3" :key="v" class="re-main">
            <div v-if="boutiqueAppList[index]" @click="openDetails(boutiqueAppList[index].id)">
              <div class="re-banner">
                <el-image
                  v-if="boutiqueAppList[index].boutiqueFileId"
                  :src="baseUrl + '/market/downloadFileById?id=' + boutiqueAppList[index].boutiqueFileId + '&Authorization=' + token"
                  :z-index="2003"
                  style="width: 100%;height: 100%;"
                />
                <img v-else :src="boutique" style="width: 100%;height: 100%;">
                <div v-if="boutiqueAppList[index].appCost" class="tag pay">付费</div>
                <div v-else class="tag">{{ $t('免费') }}</div>
              </div>
              <div class="re-info-flex">
                <div class="re-info-tit">
                  <div class="appName">
                    <span>{{ boutiqueAppList[index].appName }}</span>
                    <i v-if="boutiqueAppList[index].appAuthor">{{ boutiqueAppList[index].appAuthor }} </i>
                  </div>
                  <p v-if="boutiqueAppList[index].appNotes" class="dec">{{ filterHtmlTag(boutiqueAppList[index].appNotes) }}</p>
                  <p v-else />
                </div>
                <div v-if="boutiqueAppList[index].state === 0 && boutiqueAppList[index].appCost && boutiqueAppList[index].isPay === 'N' " class="free" @click.stop="buyApp(boutiqueAppList[index])">立即购买</div>
                <div v-else-if="boutiqueAppList[index].state === 0 && (!boutiqueAppList[index].appCost || boutiqueAppList[index].isPay === 'Y')" class="free" @click.stop="installInInfoHandle(boutiqueAppList[index], 1)">立即安装</div>
                <div v-else class="free" @click.stop="installInInfoHandle(boutiqueAppList[index], 0)">卸载</div>
              </div>
            </div>
            <div v-else>
              <div class="re-banner">
                <img :src="boutique" style="width: 100%;height: 100%;">
              </div>
              <div class="re-info-flex">
                <div class="re-info-tit">
                  <div class="appName">
                    <span>待添加应用</span>
                  </div>
                  <p class="dec">待管理员上架推荐应用</p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="app-header">
        <span>{{ $t('更多应用') }}</span>
      </div>
      <el-row :gutter="20">
        <el-col v-for="v in installList" :key="v.id" :span="6">
          <div class="grid-content" @click="openDetails(v.id)">
            <div v-if="v.appCost" class="tag pay">付费</div>
            <div v-else class="tag">{{ $t('免费') }}</div>
            <div class="grid-flex">
              <div class="logo">
                <img :src="v.appLogo" style="height: 100%;border-radius: 10px;">
              </div>
              <div class="re-info-flex">
                <div class="re-info-tit">
                  <div class="appName">
                    <span>{{ v.appName }}</span><i v-if="v.appAuthor">{{ v.appAuthor }}</i>
                  </div>
                  <p v-if="v.appNotes" class="dec">{{ filterHtmlTag(v.appNotes) }}</p>
                  <p v-else class="dec" />
                </div>
                <div>
                  <div v-if="v.appCost && v.isPay === 'N'" class="operate install" @click.stop="buyApp(v)">立即购买</div>
                  <div v-else class="operate install" @click.stop="installHandle(v, 1)">立即安装</div>
                </div>

              </div>
            </div>
            <div class="updadate">{{ parseTime(v.createTime) }} 上架</div>
          </div>
        </el-col>
      </el-row>
      <div class="app-header">
        <span>{{ $t('已安装应用') }}</span>
      </div>
      <el-row :gutter="20">
        <el-col v-for="v in unInstallList" :key="v.id" :span="6">
          <div class="grid-content" @click="openDetails(v.id)">
            <div class="grid-flex">
              <div class="logo">
                <img :src="v.appLogo" style="height: 100%;border-radius: 10px;">
              </div>
              <div class="re-info-flex">
                <div class="re-info-tit">
                  <div class="appName">
                    <span>{{ v.appName }}</span><i v-if="v.appAuthor">{{ v.appAuthor }}</i>
                  </div>
                  <p v-if="v.appNotes" class="dec">{{ filterHtmlTag(v.appNotes) }}</p>
                  <p v-else class="dec" />
                </div>
                <div v-if="v.state === 2" class="operate install" @click.stop="installHandle(v, 2)">
                  <el-badge is-dot class="item">更新</el-badge>
                </div>
                <div v-if="v.state === 1" class="operate uninstall" @click.stop="installHandle(v, 0)">
                  卸载
                </div>
              </div>
            </div>
            <div class="updadate">{{ parseTime(v.createTime) }} 上架</div>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-dialog v-el-drag-dialog v-loading="infoLoading" custom-class="app-dialog" title="" :visible.sync="appOpen" width="70%" :close-on-click-modal="false" :lock-scroll="false" append-to-body>
      <div class="app-details">
        <div class="app-details-left">
          <div class="top">
            <img v-if="appInfo.appLogo" :src="appInfo.appLogo" class="infoLogo">
            <i v-else class="iconfont icon-shoujizhuti_20yingyongshichang" />
            <div class="name">
              <h2>{{ appInfo.appName }}</h2>
              <p v-if="appInfo.appNotes" class="appInfoClamp">{{ filterHtmlTag(appInfo.appNotes) }}</p>
              <p v-else class="appInfoClamp" />
            </div>
            <div>
              <i v-if="appInfo.state == 0 && appInfo.appCost && appInfo.isPay === 'N'" class="operate install" @click="buyApp(appInfo)">立即购买</i>
              <i v-else-if="appInfo.state == 0 && (!appInfo.appCost || appInfo.isPay === 'Y')" class="operate install" @click="installInInfoHandle(appInfo, 1)">立即安装</i>
              <i v-else-if="appInfo.state == 2" class="operate uninstall" @click="installInInfoHandle(appInfo, 2)">更新</i>
              <i v-if="appInfo.state !== 0" class="operate uninstall" @click="installInInfoHandle(appInfo, 0)">卸载</i>
              <!-- <i class="el-icon-refresh" /> -->
            </div>

          </div>
          <div class="scroll">
            <div class="app-pic">
              <div class="banner">
                <div v-if="appInfo.fileInfoList.length > 0">
                  <el-carousel indicator-position="outside">
                    <el-carousel-item v-for="img in appInfo.fileInfoList" :key="img.id" height="200px">
                      <!-- 轮播图片 -->
                      <el-image
                        :src="baseUrl + '/market/downloadFileById?id=' + img.id + '&Authorization=' + token"
                        :preview-src-list="[baseUrl + '/market/downloadFileById?id=' + img.id + '&Authorization=' + token]"
                        :z-index="2003"
                      />
                    <!-- <el-image
                      :src="require('@/assets/image/banner.png')"
                      :z-index="2003"
                    /> -->
                    </el-carousel-item>
                  </el-carousel>
                </div>
                <div v-else>
                  <el-image
                    height="200px"
                    :src="require('@/assets/image/banner.png')"
                    :z-index="2003"
                  />
                </div>
              </div>
            </div>
            <div class="introduce">
              <h3>简介</h3>
              <p v-if="appInfo.appNotes" v-html="appInfo.appNotes" />
              <p v-else> 安信立融成立于2012年，核心团队来自中信产业基金、万达金融集团、泰康资管、埃森哲、佳兆业等。旗下产品J博士是安信立融历时10年以上研发的投资管理系统，产品体系覆盖募、投、管、退等各个环节，包含投管系统，子基金填报门户，LP门户，被投企业门户等。目前，安信立融已与全国社保、浦发银行、复星集团、华为投资、国开金融等260多家知名机构建立了良好的合作关系，致力于为行业伙伴提供优质的投资管理整体解决方案，做投资机构看不见的合伙人。</p>
              <div v-if="appInfo.updateNotes">
                <h3>最新更新</h3>
                <div v-html="appInfo.updateNotes" />
              </div>
            </div>
          </div>
        </div>
        <div class="app-details-right">
          <h2>应用提供商</h2>
          <p><i class="icon iconfont icon-gongsi" /> {{ appInfo.appProvider?appInfo.appProvider:'北京安信立融科技股份有限公司' }}</p>
          <div class="line" />
          <h2>应用类别</h2>
          <p><i class="icon iconfont icon-leixing" />
            {{ appInfo.appType?appInfo.appType:'工作协同' }}
          </p>
          <div class="line" />
          <h2>应用定价</h2>
          <p><i class="icon iconfont icon-lilvdingjia" />
            {{ appInfo.appStandard?appInfo.appStandard:'免费产品，具体定价以咨询为准' }}
          </p>
          <div class="line" />
          <h2>咨询方式</h2>
          <p><i class="icon iconfont icon-24gf-phoneBubble" /> 电话:{{ appInfo.appPhonenumber?appInfo.appPhonenumber:' 010-80537731' }}</p>
          <!-- <p><i class="icon el-icon-office-building" /> 微信: 010-000000000</p> -->
          <p><i class="icon iconfont icon-youxiang" /> 邮箱:{{ appInfo.appEmail?appInfo.appEmail:' contact@anxinlirong.com' }}</p>
          <div v-if="appInfo.menuPathText && appInfo.state !== 0">
            <div class="line" />
            <h2>安装位置</h2>
            <p><i class="icon el-icon-position" /> <a class="link_a mr5" @click="openApp(appInfo)">{{ appInfo.menuPathText }}</a></p>
          </div>
          <div class="line" />
          <h2>应用终端</h2>
          <p><i class="icon iconfont icon-leixing" />
            <span v-if="appInfo.appTerminal === 2">手机端</span>
            <span v-else-if="appInfo.appTerminal === 3">手机、PC通用</span>
            <span v-else>PC</span>
          </p>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getMarketList, installApp, getAppInfo, getBoutiqueApps } from '@/api/market/index'
import { config } from '@/utils'
import { getToken } from '@/utils/auth'
import router from '../../router'
import boutique from '@/assets/image/boutique.jpg'
export default {
    data() {
        return {
            boutique,
            dataList: [],
            installList: [],
            unInstallList: [],
            loading: false,
            appOpen: false,
            appInfo: {
              fileInfoList: []
            },
            infoLoading: false,
            imagesList: [
            ],
            token: getToken(),
            baseUrl: config.baseURL,
            boutiqueAppList: []
        }
    },
    mounted() {
        this.getList()
    },
    methods: {
        filterHtmlTag(html) {
          var msg = html.replace(/<\/?[^>]*>/g, '')
          msg = msg.replace(/[|]*\n/, '')
          return msg
        },
        getList() {
          this.loading = true
          this.installList = []
          this.unInstallList = []
          getBoutiqueApps().then(res => {
              if (res.code === 200 && res.rows.length > 0) {
                this.boutiqueAppList = res.rows
              }
          }).catch(() => {
            this.loading = true
          })
          getMarketList().then(res => {
              if (res.code === 200 && res.rows.length > 0) {
                var rows = res.rows
                rows.forEach(v => {
                  if (v.state === 0) { // state: 0为卸载，1为安装
                    this.installList.push(v)
                  } else {
                    this.unInstallList.push(v)
                  }
                })
                this.loading = false
              }
          }).catch(() => {
            this.loading = true
          })
        },
        installHandle(data, state) {
          // 禁止卸载JStore
          if (data.appName === 'JStore') {
            this.$message.warning('请联系管理员卸载JStore!')
            return
          }
          this.loading = true
          // console.log(data)
          var row = {
            appId: data.id,
            state: state
          }
          installApp(row).then(res => {
              if (res.code === 200) {
                  const pathText = res.data.menuPathText
                  // console.log(res)
                  this.$message.success('操作成功')
                  if (state === 1 && res.data.menuPath) {
                    const notify = this.$notify({
                      title: '安装成功',
                      dangerouslyUseHTMLString: true,
                      type: 'success',
                      duration: 0,
                      // message: '安装成功，点击跳转',
                      message: '<a href="#">安装成功，点击跳转<p style="color:blue;text-decoration: underline;" >' + pathText + ' </p></a>',
                      onClick: function() {
                        const routeUrl = router.resolve({
                          path: '/pathHandle',
                          query: { path: res.data.menuPath }
                        })
                        notify.close()
                        window.open(routeUrl.href, '_blank')
                        // router.go(0)
                      }
                    })
                  } else {
                    // this.getList()
                    // setTimeout(() => {
                    //   this.$router.go(0)
                    // }, 500)
                  }
                  this.getList()
              }
          }).catch(() => {
            this.loading = false
          })
        },
        installInInfoHandle(data, state) {
          console.log(data)
          this.appOpen = false
          this.installHandle(data, state)
        },
        openDetails(appId) {
          this.appOpen = true
          this.infoLoading = true
          getAppInfo(appId).then(res => {
              if (res.code === 200) {
                this.appInfo = res.data
                console.log(this.appInfo)
                this.infoLoading = false
              }
          }).catch(() => {
            this.loading = true
          })
        },
        openApp(data) {
          const routeUrl = router.resolve({
            path: data.menuPath
          })
          window.open(routeUrl.href, '_blank')
        },
        buyApp(data) {
          const routeUrl = this.$router.resolve({
            path: '/qrcode',
            query: { appId: data.id, appCost: data.appCost, appName: data.appName }
          })
          window.open(routeUrl.href, '_blank')
        }
    }
}
</script>
<style lang="scss" scoped>
.m--15{
  margin: -15px
}
.home .banner {
  background-color: #99a9bf;
  // background: url() center center no-repeat;
  background: url('~@/assets/image/JStore/JStore.png') center center no-repeat;
  // background: url() center center no-repeat;
  height: 100%;
  // min-width: 1100px;
}
.pad90{
  padding: 0px 90px;
}
.recommend{
  padding: 56px 0px 40px;
  .b-tit{
    display: flex;
    align-items: center;
    margin-left: 8px;
    font-size: 20px;
    line-height: 32px;
    color: rgb(14, 17, 20);
    font-weight: 600;
    margin-bottom: 20px;
    i{
      color: #FFAB24;
      font-size: 25px;
    }
  }
  .re-main-flex{
    display: flex;
    column-gap: 20px;
    .re-main{
      flex: 1;
      min-width: 356px;
      height: 296px;
      background: rgb(255, 255, 255);
      box-sizing: border-box;
      border-radius: 3px;
      overflow: hidden;
      cursor: pointer;
      .re-banner{
        position: relative;
        width: 100%;
        height: 190px;
        // background: url(https://dfiles.tms.beisen.com/Portal/110006/597519f00d7341b4897c5c8be427557b.png) center center / cover no-repeat;
        background: center center / cover no-repeat;
      }

    }
  }
}
.tag{
  position: absolute;
  width: 110px;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  left: -35px;
  top: 9px;
  padding: 5px 0 5px;
  transform: rotate(-45deg);
  background: #46CF84;
  color: #fff;
  font-size: 12px;
  zoom: 0.75;
}
.pay{
  background: #FE586A;
}
.re-info-flex{
  display: flex;
  padding: 24px;
  .re-info-tit{
    flex: 1;
    width: calc(100% - 100px);
  }
  .appName{
    display: flex;
    align-items: center;
    margin-bottom: 10px;
    span{
      display: inline-block;
      font-weight: 600;
      vertical-align: middle;
      font-size: 18px;
      line-height: 26px;
      color: rgb(51, 56, 61);
      text-overflow: ellipsis;
      overflow: hidden;
      white-space: nowrap;
    }
    i{
      font-style: normal;
      font-size: 11px;
      border-radius: 4px;
      border: 1px solid #EEF0F2;
      color: #6d6d6d;
      padding: 2px 3px;
      zoom: 0.9;
      margin-left: 8px;
      white-space: nowrap;
      text-overflow: clip;
      overflow: hidden;
    }
  }
  .free{
    display: inline-block;
    height: 30px;
    padding: 0px 15px;
    line-height: 30px;
    font-size: 12px;
    text-align: center;
    color: rgb(255, 255, 255);
    border-radius: 15px;
    background: #1890ff;
    cursor: pointer;
    box-sizing: border-box;
  }
  .dec{
    font-size: 12px;
    line-height: 22px;
    color: rgb(131, 137, 143);
    margin: 0px;
    padding: 0px;
    width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
.app-header{
  font-size: 16px;
  line-height: 28px;
  color: rgb(14, 17, 20);
  display: flex;
  -webkit-box-align: center;
  align-items: center;
  -webkit-box-pack: justify;
  justify-content: space-between;
  font-weight: 500;
  margin-bottom: 20px;
}
/deep/.app-dialog{
  background: #f6f7f9;
  padding: 20px;
  height: 80%;
  top: 10%!important;
  margin-top: 0!important;
  left: 0%!important;
  .el-dialog__header, .el-dialog__body{
    padding: 0!important;
    border: 0;
  }
  .el-dialog__body{
    height: 100%;
  }
  .app-details{
    height: 100%;
    display: flex;
    column-gap: 20px;
    .line{
      margin: 20px 0;
      border-top: 1px solid #F5F5F7;
    }
    .app-details-left{
      flex: 1;
      background: #fff;
      padding: 20px;
      border-radius: 4px;
      max-width: calc(100% - 320px);
      .scroll{
        height: calc(100% - 90px);
        overflow-y: auto;
        margin-right: -20px;
        padding-right: 20px;
      }
      .top{
        display: flex;
        align-items: center;
        column-gap: 7px;
        border-bottom: 2px dashed rgb(240, 242, 245);
        padding-bottom: 20px;
        margin-bottom: 25px;
        justify-content: space-between;
        i{
          cursor: pointer;
        }
        .iconfont{
          color: #FF5C66;
          font-size: 45px;
        }
        .name{
          flex: 1;
          max-width: calc(100% - 200px);
          .iconfont {
            font-size: 45px;
            color: #FE424B;
          }
          h2{
            display: inline-block;
            font-weight: 600;
            vertical-align: middle;
            font-size: 18px;
            line-height: 26px;
            color: #33383d;
            text-overflow: ellipsis;
            overflow: hidden;
            white-space: nowrap;
            margin: 0;
            margin-bottom: 6px;
          }
          p{
            font-size: 12px;
            line-height: 22px;
            color: #83898f;
            margin: 0px;
            padding: 0px;
            width: 100%;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            display: block;
          }
        }
      }
      .app-pic{
      }
      h3{
        font-size: 15px;
        margin: 0;
        color: #000;
        margin: 20px 0 10px;
      }
      p{
        color: rgba(0, 0, 0, 0.75);
        font-size: 13px;
        margin: 0;
        line-height: 20px;
      }
    }
    .app-details-right{
      padding: 20px;
      border-radius: 4px;
      width: 300px;
      background: #fff;
      h2{
        font-size: 15px;
        margin: 0;
        color: #33383d;
      }
      p{
        color: #565e66;
        font-size: 13px;
        margin: 8px 0;
        line-height: 20px;
        i{
          color: #CFD2D5;
        }
      }
    }
  }
}

.grid-content{
    border-radius: 6px;
    margin-bottom: 20px;
    background: #fff;
    padding: 24px 20px 0px 24px;
    position: relative;
    height: 150px;
    overflow: hidden;
    .re-info-flex{
      padding: 0;
      flex: 1;
      max-width: calc(100% - 70px);
      display: flex;
      justify-content: space-between;
      .appName{
        margin-bottom: 6px
      }
      .re-info-tit{
        max-width: calc(100% - 70px);
      }
    }
    .grid-flex{
      display: flex;
      align-items: center;
      padding-bottom: 20px;
    }
    .updadate{
      padding: 12px 0px;
      font-size: 12px;
      line-height: 20px;
      color: rgb(131, 137, 143);
      border-top: 1px solid rgb(240, 242, 245);
    }
    .logo{
      width: 60px;
      height: 60px;
      border-radius: 10px;
      margin-right: 10px;
      i{
        font-size: 40px;
        color: #bebebe;
      }
    }

    .app-con{
        flex: 1;
        p{
            margin: 0;
        }
        .tit{
            font-size: 14px;
            font-weight: bold;
            margin-bottom: 10px;
        }
        .dec{
            font-size: 12px;
            color: #9EA4A6;
        }
    }
}
.area-header{
    margin-bottom: 20px;
}

.infoLogo{
  width: 60px;
  height: 60px;
  i{
    font-size: 40px;
    color: #bebebe;
  }
}
.operate{
  display: inline-block;
  height: 26px;
  padding: 0px 10px;
  line-height: 26px;
  font-size: 12px;
  text-align: center;
  border-radius: 13px;
  cursor: pointer;
  box-sizing: border-box;
  white-space: nowrap;
  font-style: normal;
}
.install{
  color: rgb(21, 129, 238);
  background: rgba(21, 129, 239, 0.05);
}
.uninstall{
  color: rgb(254,81,99);
  background: rgba(254,81,99, 0.05);
}
/deep/.appInfoClamp{
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
  text-overflow: ellipsis;
  p{
    margin: 0;
    // display: inline-block;
  }
}
.appBuy{
  display: block;
    text-align: center;
    font-size: 6px;
    margin-top: 3px;
    color: #999;
}
/deep/.ql-align-justify{
  margin: 0;
}

.grid-content:hover, .re-main:hover{
  box-shadow: rgba(0,0,0,0.05) 0px 0px 8px 0px;
}
</style>
